package com.huixue.cn.ztej.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.huixue.cn.ztej.base.Constant;

/**
 * This utility provides some convenient method for string operation
 * 
 * @author mayanjun
 * @version 1.0
 */
public final class Strings {
	/**
	 * The pattern of email
	 */
	public static final Pattern PATTERN_EMAIL = Pattern
			.compile(
					"^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$",
					Pattern.CASE_INSENSITIVE);

	private Strings() {
	}

	/**
	 * Test if a string is empty
	 * 
	 * @param src
	 * @return
	 */
	public static boolean isEmpty(String src) {
		return src == null || src.trim().length() == 0;
	}

	/**
	 * Test if a string is email
	 * 
	 * @param src
	 * @return
	 */
	public static boolean isEmail(String src) {
		if (src != null && src.length() > 0)
			return src.matches(PATTERN_EMAIL.pattern());
		return false;
	}

	/**
	 * Test a string is number
	 * 
	 * @param src
	 * @return
	 */
	public static boolean isNumber(String src) {
		try {
			Double.parseDouble(src);
			return true;
		} catch (NumberFormatException e) {
		}
		return false;
	}

	/**
	 * Returns a uuid string
	 * 
	 * @return
	 */
	public static String uuid() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	public static String formatDate(long time) {
		return Constant.COMMON_DATA_FORMAT1.format(new Date(time));
	}

	public static String formatDate(Date date) {
		return Constant.COMMON_DATA_FORMAT1.format(date);
	}

	public static String formatDate1(long time) {
		return Constant.COMMON_DATA_FORMAT1.format(new Date(time));
	}

	public static String formatDate1(Date date) {
		return Constant.COMMON_DATA_FORMAT1.format(date);
	}

	/**
	 * Counts lines of file
	 * 
	 * @param path
	 * @return
	 */
	public static int wc(String path) {
		int lines = 0;
		try {
			String command = "wc -l " + path;
			String os = System.getProperty("os.name").toLowerCase();
			if (os.contains("windows")) {
				command = "type " + path + "|find /C /V \"\"";
			}
			Process process = Runtime.getRuntime().exec(command);
			InputStream in = process.getInputStream();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int read = -1;
			while ((read = in.read()) != -1) {
				baos.write(read);
			}
			String s = new String(baos.toByteArray());
			Pattern p = Pattern.compile("\\d+");
			Matcher m = p.matcher(s);
			if (m.find())
				s = m.group();
			lines = Integer.parseInt(s);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return lines;
	}

	/**
	 * 
	 * @return
	 */
	private static final SimpleDateFormat TSSDF = new SimpleDateFormat(
			"yyyyMMdd");

	public static String timestamp() {
		return TSSDF.format(new Date());
	}

	/**
	 * 判断密码，必须包含字母和数字并且长度在6到16位之间
	 * 
	 * @param src
	 * @param min
	 * @param max
	 * @return
	 */
	public static boolean isPassword(String src, int min, int max) {
		int ret = 0;
		if (src == null || src.trim().length() == 0)
			return false;
		if (Pattern.matches(".*[a-zA-Z]+.*", src))
			++ret;
		if (Pattern.matches(".*[0-9]+.*", src))
			++ret;
		int len = src.trim().length();
		if (len >= min && len <= max)
			++ret;
		if (ret >= 3)
			return true;
		return false;
	}

	public static String arrayToString(String list[], String sep) {
		if (list == null || list.length == 0)
			return null;
		if (sep == null)
			sep = ",";
		StringBuilder sb = new StringBuilder();
		int len = list.length;
		for (int i = 0; i < len; i++) {
			sb.append(list[i]);
			if (i < (len - 1))
				sb.append(sep);
		}
		return sb.toString();
	}

	public static String maskString(String src, int start, int offset) {
		if (src != null && start > 0 && offset > 0 && src.length() > start) {
			StringBuilder sb = new StringBuilder(src);
			for (int i = start - 1; i < sb.length() && offset > 0; i++, offset--) {
				sb.setCharAt(i, '*');
			}
			return sb.toString();
		}
		return src;
	}

	/**
	 * 驼峰式写法与下划线写法的转换
	 * 
	 * @param param
	 * @return
	 */
	public static String camel4underline(String param) {
		Pattern p = Pattern.compile("[A-Z]");
		if (param == null || param.equals("")) {
			return "";
		}
		StringBuilder builder = new StringBuilder(param);
		Matcher mc = p.matcher(param);
		int i = 0;
		while (mc.find()) {
			builder.replace(mc.start() + i, mc.end() + i, "_" + mc.group().toLowerCase());
			i++;
		}

		if ('_' == builder.charAt(0)) {
			builder.deleteCharAt(0);
		}
		return builder.toString();
	}

	/**
	 * 首字母如果大写改为小写
	 * 
	 * @param str
	 * @return
	 */
	public static String changeString(String str) {
		String first = str.substring(0, 1);
		Pattern p = Pattern.compile("[A-Z]");
		Matcher mc = p.matcher(first);
		if (mc.find()) {
			return first.toLowerCase() + str.substring(1);
		} else {
			return str;
		}
	}

}
